package com.example.demo;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
    // 用 List 模拟数据库
    private List<String> userList = new ArrayList<>();
    private Integer nextId = 1; // 自增 id 简单模拟

    public UserController() {
        // 初始化两个用户
        userList.add( "Alice");
        userList.add( "Bob");
    }
    // 1）查询所有用户
    @GetMapping
    public List<String> listUsers() {
        return userList;
    }
    // 2）根据 id 查询单个用户
    @GetMapping("/{id}")
    public String getUser(@PathVariable("id") Integer id) {
        return userList.get(id);
    }
    // 3）新增用户
    @GetMapping("/add/{name}")
    public List<String> createUser(@PathVariable String name) {
        userList.add(name);
        return userList;
    }
    //修改用户
    @PutMapping("/{id}/{name}")
    public List<String>  updateUser(@PathVariable("id") Integer id,
                                    @PathVariable("name") String name){
      userList.set(id,name);
      return userList;
        }
    // 5）删除用户
    @DeleteMapping("/{id}")
    public  List<String>  deleteUser(@PathVariable("id") String name) {
        userList.remove(name);
        return userList;
        }
}